<template>
  <el-dialog
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :title="title"
    :visible.sync="dialogFormVisible"
    width="500px"
    @close="close"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="分组号" prop="group">
        <el-input-number v-model.number="form.group" />
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input-number v-model.number="form.sort" />
      </el-form-item>
      <el-form-item label="按钮图片" prop="link_image">
        <img-upload
          path="carousel"
          :base-api="baseApi"
          :src="form.link_image ? baseUrl + form.link_image : ''"
          @update="form.link_image = $event"
        />
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-select v-model="form.type" placeholder="请选择">
          <el-option
            v-for="item in types"
            :key="item.value"
            :label="item.display_name"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="跳转地址" prop="link_url">
        <el-input v-model.trim="form.link_url" />
      </el-form-item>
      <el-form-item label="链接标志" prop="link_flag">
        <el-select v-model="form.link_flag" placeholder="请选择">
          <el-option
            v-for="item in link_flags"
            :key="item.value"
            :label="item.display_name"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { doEdit, doCreate } from '@/api/carousel'
  import { link_flags, types } from '../../config'
  import ImgUpload from '@/components/ImgUpload'

  export default {
    name: 'CarouselEdit',
    components: {
      ImgUpload,
    },
    data() {
      return {
        id: null,
        form: {
          group: null,
          sort: null,
          link_image: '',
          type: null,
          link_url: '',
          link_flag: null,
        },
        rules: {
          group: [{ required: true, trigger: 'blur', message: '请输入分组号' }],
          type: [{ required: true, trigger: 'blur', message: '请输选择类型' }],
        },
        title: '',
        dialogFormVisible: false,
        link_flags,
        types,
        baseApi: '/_topic/',
        baseUrl: '',
      }
    },
    created() {},
    methods: {
      showEdit(row) {
        if (!row) {
          this.title = '添加'
        } else {
          this.title = '编辑'
          this.form = Object.assign({}, row)
          this.id = row.id
        }
        this.dialogFormVisible = true
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
        this.id = null
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            if (this.id) {
              await doEdit(this.baseApi, this.form)
              this.$baseMessage('编辑成功', 'success')
            } else {
              await doCreate(this.baseApi, this.form)
              this.$baseMessage('新增成功', 'success')
            }
            this.$emit('fetch-data')
            this.close()
          } else {
            return false
          }
        })
      },
    },
  }
</script>
